package net.alteiar.ui.algorithms;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class PanelAlgorithmClustering extends
		PanelAlgorithmsDetails<AlgorithmClustering> {
	private static final long serialVersionUID = 1L;

	private final JSpinner spinnerCluster;
	private final JSpinner spinnerPrecision;
	private final ColorsPanel colorsPanel;

	public PanelAlgorithmClustering() {
		super(new AlgorithmClustering());
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 68, 56, 29, 0 };
		gridBagLayout.rowHeights = new int[] { 30, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JLabel label = new JLabel("Cluster count:");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.WEST;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 0;
		gbc_label.gridy = 0;
		add(label, gbc_label);

		spinnerCluster = new JSpinner();
		spinnerCluster.setModel(new SpinnerNumberModel(new Integer(5),
				new Integer(2), null, new Integer(1)));
		GridBagConstraints gbc_spinner = new GridBagConstraints();
		gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
		gbc_spinner.insets = new Insets(0, 0, 5, 5);
		gbc_spinner.gridx = 1;
		gbc_spinner.gridy = 0;
		add(spinnerCluster, gbc_spinner);

		JLabel label_1 = new JLabel("Pixel precision:");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.anchor = GridBagConstraints.WEST;
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 0;
		gbc_label_1.gridy = 1;
		add(label_1, gbc_label_1);

		spinnerPrecision = new JSpinner();
		spinnerPrecision.setModel(new SpinnerNumberModel(new Integer(1),
				new Integer(1), null, new Integer(1)));
		GridBagConstraints gbc_spinnerPrecision = new GridBagConstraints();
		gbc_spinnerPrecision.fill = GridBagConstraints.HORIZONTAL;
		gbc_spinnerPrecision.insets = new Insets(0, 0, 5, 5);
		gbc_spinnerPrecision.gridx = 1;
		gbc_spinnerPrecision.gridy = 1;
		add(spinnerPrecision, gbc_spinnerPrecision);

		colorsPanel = new ColorsPanel();
		colorsPanel.setColors(2);
		GridBagConstraints gbc_colorsPanel = new GridBagConstraints();
		gbc_colorsPanel.gridwidth = 3;
		gbc_colorsPanel.insets = new Insets(0, 0, 0, 5);
		gbc_colorsPanel.anchor = GridBagConstraints.NORTHWEST;
		gbc_colorsPanel.gridx = 0;
		gbc_colorsPanel.gridy = 2;
		add(colorsPanel, gbc_colorsPanel);

		// init algo
		getAlgorithm().setClusterCount((int) spinnerCluster.getValue());
		getAlgorithm().setPrecision((int) spinnerPrecision.getValue());

		getAlgorithm().setColors(colorsPanel.getColors());
	}

	@Override
	protected void updateParams() {
		AlgorithmClustering algo = getAlgorithm();
		algo.setClusterCount((int) spinnerCluster.getValue());
		algo.setPrecision((int) spinnerPrecision.getValue());

		algo.setColors(colorsPanel.getColors());
	}

}
